package com.meida.module.bus.provider.utils;

import com.alibaba.fastjson.util.TypeUtils;
import com.meida.common.utils.FlymeUtils;
import com.meida.common.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class LevelNoUtil {
    @Autowired
    RedisUtils redisUtils;

    public synchronized String getLevelNo(String redisKey, String packageNo, String levelNo) {
        String maxNumStr = redisUtils.getString(redisKey);
        int startLength = packageNo.length();
        int endLength = startLength + 4;
        String parentSerialNumber = levelNo.substring(startLength, endLength);
        String levelNoPrefix = packageNo + parentSerialNumber;
        if (FlymeUtils.isEmpty(maxNumStr)) {
            maxNumStr=levelNo.substring(9, 11);
            Integer maxNum = TypeUtils.castToInt(maxNumStr) + 1;
            maxNumStr = TypeUtils.castToString(maxNum);
            if (maxNum < 10) {
                maxNumStr = "0" + maxNum;
            }
            redisUtils.set(redisKey, maxNumStr);
        } else {
            Integer maxNum = TypeUtils.castToInt(maxNumStr) + 1;
            maxNumStr = TypeUtils.castToString(maxNum);
            if (maxNum < 10) {
                maxNumStr = "0" + maxNum;
            }
            redisUtils.set(redisKey, maxNumStr);
        }
        return levelNoPrefix + maxNumStr;
    }

    public static void main(String[] args) {
        String levelNo = "P02J3000300";
        String packageNo = "P02J3";
        String parentSerialNumber = levelNo.substring(packageNo.length(), packageNo.length() + 4);
        System.out.println(parentSerialNumber);
    }
}
